package com.eden.util;

import cn.hutool.core.util.StrUtil;
import com.eden.entity.BasisInfo;
import com.eden.entity.PropertyInfo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 链接数据库并获取表信息
 */
public class EntityInfoUtil {
	public static BasisInfo getInfo(BasisInfo bi) throws SQLException {
		List<PropertyInfo> columns = new ArrayList<PropertyInfo>();
		String primaryKeyFieldName = "";
		// 创建连接
		Connection con = null;
		PreparedStatement pstemt = null;
		ResultSet rs = null;
		//sql
		String sql = "select column_name,data_type,column_comment from information_schema.columns where table_schema='" + bi.getDatabase() + "' and table_name='" + bi.getTable() + "'";
		try {
			con = DriverManager.getConnection(bi.getDbUrl(), bi.getDbName(), bi.getDbPassword());
			pstemt = con.prepareStatement(sql);
			ResultSet primaryKeys = con.getMetaData().getPrimaryKeys(con.getCatalog(), null, bi.getTable());
			rs = pstemt.executeQuery();
			// 实体信息主键获取 -- 仅支持单主键模式
			if (primaryKeys.next()) {
				bi.setHasPrimaryKey(true);
				primaryKeyFieldName = primaryKeys.getString(4);
			}
			while (rs.next()) {
				String fieldName = rs.getString(1);
				String jdbcType = rs.getString(2);
				String comment = rs.getString(3);
				PropertyInfo ci = new PropertyInfo();
				ci.setFieldName(fieldName);
				// 设置字段首字母大写
				ci.setUpperFieldName(StrUtil.upperFirst(fieldName));
				if (jdbcType.equalsIgnoreCase("int")) {
					ci.setJdbcType("Integer");
				} else if (jdbcType.equalsIgnoreCase("datetime")) {
					ci.setJdbcType("timestamp");
				} else {
					ci.setJdbcType(jdbcType);
				}
				// 设置主体主键的信息
				if (fieldName.equals(primaryKeyFieldName)) {
					bi.setPrimaryKeyFieldName(primaryKeyFieldName);
					bi.setPrimaryKeyPropertyName(MySqlToJavaUtil.changeToJavaFiled(primaryKeyFieldName));
					bi.setPrimaryKeyDataBaseType(jdbcType);
					bi.setPrimaryKeyType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));
				}
				ci.setComment(comment);
				ci.setProperty(MySqlToJavaUtil.changeToJavaFiled(fieldName));
				ci.setJavaType(MySqlToJavaUtil.jdbcTypeToJavaType(jdbcType));
				columns.add(ci);
				//添加包路径
				Set<String> pkgs = bi.getPkgs();
				pkgs.add(MySqlToJavaUtil.jdbcTypeToJavaTypePck(jdbcType));
				bi.setPkgs(pkgs);
			}
			// 设置实体类名首字母小写
			bi.setLowerEntityName(StrUtil.lowerFirst(bi.getEntityName()));
			bi.setCis(columns);
			//
			// 完成后关闭
			rs.close();
			pstemt.close();
			con.close();
			if (null == columns || columns.size() == 0) {
				throw new RuntimeException("未能读取到表或表中的字段。请检查链接url，数据库账户，数据库密码，查询的数据名、是否正确。");
			}
			return bi;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("自动生成实体类错误：" + e.getMessage());
		} finally {
			try {
				if (rs != null) rs.close();
			} catch (SQLException se2) {
			}
			// 关闭资源
			try {
				if (pstemt != null) pstemt.close();
			} catch (SQLException se2) {
			}// 什么都不做
			try {
				if (con != null) con.close();
			} catch (SQLException se) {
				se.printStackTrace();
			}
		}
	}
}